#include "math_util.h"

//
// Sum power
//
double sum_power(int num_elements, double* xi, int power)
{
	double sum = 0;
	for (int i = 0; i < num_elements; i++)
	{
		double pow = 1.0;
		for (int j = 0; j < power; j++)
			pow *= xi[i];
		sum += pow;
	}

	return sum;
}

//
// Sum multiply power
//
double sum_mul_power(int num_elements, double* xi, double* yi, int power_x, int power_y)
{
	double sum = 0;
	for (int i = 0; i < num_elements; i++)
	{
		double pow_x = 1.0;
		for (int j = 0; j < power_x; j++)
			pow_x *= xi[i];

		double pow_y = 1.0;
		for (int j = 0; j < power_y; j++)
			pow_y *= yi[i];

		sum += pow_x * pow_y;
	}

	return sum;
}

//
// Evaluate polynimials
//
double evaluate_polynomial(double x, double* p, int num_p)
{
	double sum = 0.0;
	for (int i = 0; i < num_p; i++)
	{
		double pow = 1.0;
		for (int pp = i; pp > 0; pp--)
			pow *= x;
		sum += p[num_p - i - 1] * pow;
	}

	return sum;
}